package com.wqzhang.thingswapper.dao.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "TO_DO_THING".
*/
public class ToDoThingDao extends AbstractDao<ToDoThing, Long> {

    public static final String TABLENAME = "TO_DO_THING";

    /**
     * Properties of entity ToDoThing.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property ReminderContext = new Property(1, String.class, "reminderContext", false, "REMINDER_CONTEXT");
        public final static Property ReminderType = new Property(2, Integer.class, "reminderType", false, "REMINDER_TYPE");
        public final static Property CreateDate = new Property(3, java.util.Date.class, "createDate", false, "CREATE_DATE");
        public final static Property FinshDate = new Property(4, java.util.Date.class, "finshDate", false, "FINSH_DATE");
        public final static Property Status = new Property(5, Integer.class, "status", false, "STATUS");
        public final static Property Synchronize = new Property(6, Boolean.class, "synchronize", false, "SYNCHRONIZE");
        public final static Property ServiceId = new Property(7, Long.class, "serviceId", false, "SERVICE_ID");
        public final static Property UserId = new Property(8, Long.class, "userId", false, "USER_ID");
    }

    private DaoSession daoSession;

    private Query<ToDoThing> user_ToDoThingsQuery;

    public ToDoThingDao(DaoConfig config) {
        super(config);
    }
    
    public ToDoThingDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"TO_DO_THING\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"REMINDER_CONTEXT\" TEXT," + // 1: reminderContext
                "\"REMINDER_TYPE\" INTEGER," + // 2: reminderType
                "\"CREATE_DATE\" INTEGER," + // 3: createDate
                "\"FINSH_DATE\" INTEGER," + // 4: finshDate
                "\"STATUS\" INTEGER," + // 5: status
                "\"SYNCHRONIZE\" INTEGER," + // 6: synchronize
                "\"SERVICE_ID\" INTEGER," + // 7: serviceId
                "\"USER_ID\" INTEGER);"); // 8: userId
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"TO_DO_THING\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ToDoThing entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String reminderContext = entity.getReminderContext();
        if (reminderContext != null) {
            stmt.bindString(2, reminderContext);
        }
 
        Integer reminderType = entity.getReminderType();
        if (reminderType != null) {
            stmt.bindLong(3, reminderType);
        }
 
        java.util.Date createDate = entity.getCreateDate();
        if (createDate != null) {
            stmt.bindLong(4, createDate.getTime());
        }
 
        java.util.Date finshDate = entity.getFinshDate();
        if (finshDate != null) {
            stmt.bindLong(5, finshDate.getTime());
        }
 
        Integer status = entity.getStatus();
        if (status != null) {
            stmt.bindLong(6, status);
        }
 
        Boolean synchronize = entity.getSynchronize();
        if (synchronize != null) {
            stmt.bindLong(7, synchronize ? 1L: 0L);
        }
 
        Long serviceId = entity.getServiceId();
        if (serviceId != null) {
            stmt.bindLong(8, serviceId);
        }
 
        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(9, userId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ToDoThing entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String reminderContext = entity.getReminderContext();
        if (reminderContext != null) {
            stmt.bindString(2, reminderContext);
        }
 
        Integer reminderType = entity.getReminderType();
        if (reminderType != null) {
            stmt.bindLong(3, reminderType);
        }
 
        java.util.Date createDate = entity.getCreateDate();
        if (createDate != null) {
            stmt.bindLong(4, createDate.getTime());
        }
 
        java.util.Date finshDate = entity.getFinshDate();
        if (finshDate != null) {
            stmt.bindLong(5, finshDate.getTime());
        }
 
        Integer status = entity.getStatus();
        if (status != null) {
            stmt.bindLong(6, status);
        }
 
        Boolean synchronize = entity.getSynchronize();
        if (synchronize != null) {
            stmt.bindLong(7, synchronize ? 1L: 0L);
        }
 
        Long serviceId = entity.getServiceId();
        if (serviceId != null) {
            stmt.bindLong(8, serviceId);
        }
 
        Long userId = entity.getUserId();
        if (userId != null) {
            stmt.bindLong(9, userId);
        }
    }

    @Override
    protected final void attachEntity(ToDoThing entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ToDoThing readEntity(Cursor cursor, int offset) {
        ToDoThing entity = new ToDoThing( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // reminderContext
            cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // reminderType
            cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)), // createDate
            cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)), // finshDate
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // status
            cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0, // synchronize
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7), // serviceId
            cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8) // userId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ToDoThing entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setReminderContext(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setReminderType(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2));
        entity.setCreateDate(cursor.isNull(offset + 3) ? null : new java.util.Date(cursor.getLong(offset + 3)));
        entity.setFinshDate(cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)));
        entity.setStatus(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setSynchronize(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
        entity.setServiceId(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
        entity.setUserId(cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ToDoThing entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ToDoThing entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ToDoThing entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "toDoThings" to-many relationship of User. */
    public List<ToDoThing> _queryUser_ToDoThings(Long userId) {
        synchronized (this) {
            if (user_ToDoThingsQuery == null) {
                QueryBuilder<ToDoThing> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.UserId.eq(null));
                user_ToDoThingsQuery = queryBuilder.build();
            }
        }
        Query<ToDoThing> query = user_ToDoThingsQuery.forCurrentThread();
        query.setParameter(0, userId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserDao().getAllColumns());
            builder.append(" FROM TO_DO_THING T");
            builder.append(" LEFT JOIN USER T0 ON T.\"USER_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ToDoThing loadCurrentDeep(Cursor cursor, boolean lock) {
        ToDoThing entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        User user = loadCurrentOther(daoSession.getUserDao(), cursor, offset);
        entity.setUser(user);

        return entity;    
    }

    public ToDoThing loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ToDoThing> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ToDoThing> list = new ArrayList<ToDoThing>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ToDoThing> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ToDoThing> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
